<?php
class test2{
    public $a=1;
}
class test{
    public $a=1;
    public $mode;
    public function __construct($a)
    {

        $this->mode=$a( new test2());
    }
    public function get(){
        return json_encode($this,JSON_FORCE_OBJECT);
    }
}

$test=new test(function ($query){
     $query->a=2;
     return $query;
});
 echo  json_encode($test,JSON_FORCE_OBJECT);
echo $test->mode->a;
$test1=$test->get();
echo $test1;
